package com.skt.common_hr.biztrip_nos.model
{
	import com.skt.common_hr.biztrip_nos.model.vo.AdjustBaseVO;
	import com.skt.common_hr.biztrip_nos.model.vo.BizTripMemberVO;
	import com.skt.common_hr.biztrip_nos.model.vo.BizTripSummaryVO;
	import com.skt.common_hr.biztrip_nos.model.vo.MemberAdjustVO;
	import com.skt.common_hr.common.approval.IRequestApprovalProxy;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.NumberUtil;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import mx.collections.ArrayCollection;

	public class DomesticApprovalProxy extends CommonProxy implements IRequestApprovalProxy
	{
		public static const NAME:String = "DomesticApprovalProxy";
		public var dataProvider:ArrayCollection;

		private var _bAppType:Boolean = false;		// 정산 포함여부
		private var _amount:Number;
		
		public var summary:BizTripSummaryVO;
		public var placeList:ArrayCollection; 
		public var memberList:ArrayCollection;
		public var adjustBase:AdjustBaseVO;
		public var memberCarfare:ArrayCollection;
		public var memberDetailCost:ArrayCollection;
		public var memberAdjustList:ArrayCollection;
		
		private var templateHTML1:String = "";
		private var templateHTML2:String = "";
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function DomesticApprovalProxy(data:Object=null)
		{
			super(NAME, data);
			loadTemplate1();
			loadTemplate2();
		}
		
		private var _key:String = "";
		public function get key():String
		{
			return _key;
		}
		public function set key(k:String):void
		{
			_key = k;
		}
		
		public function get title():String 
		{
			var tmp:String;
			tmp = summary.TRTITLE + " (" + DateUtil.toDefaultString(summary.BEGDA) + " ~ " + DateUtil.toDefaultString(summary.ENDDA) + ")"; 
			/*
			if(_bAppType) {
				tmp = "시외 출장 정산 품의";
			}
			else {
				tmp = "시외 출장 품의";
			}
			if(summary.TRTITLE.length>0) {
				tmp += "(" + summary.TRTITLE + ")";
			}
			*/
			return tmp;
		}
		
		public function get content():String
		{
			if(_bAppType) {
				return buildHTML2();	
			}
			else {
				return buildHTML(templateHTML1);
			}
		}
		
		public function get appCode():String 
		{
			return "03"; 	//
		}
 		
 		public function set appType(b:Boolean):void
 		{
 			_bAppType = b;
 		}
 		
 		public function set amount(n:Number):void
 		{
 			_amount = n;
 		}
 		public function get amount():Number
 		{
 			return _amount;
 		}
 		private function loadTemplate1():void
		{
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest("assets/template/DomesticBizTrip.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}
 			
		private function loadTemplate2():void
		{
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest("assets/template/DomesticAdjust_nos.tmp");
			loader.addEventListener(Event.COMPLETE, templateResult2);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            try {
                loader.load(request);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
		}
		
		private function templateResult(event:Event):void
		{
			templateHTML1 = event.target.data;
		}
		
		private function templateResult2(event:Event):void
		{
			templateHTML2 = event.target.data;
		}

		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
		}


		private function buildHTML(tmp:String):String
		{
			var htmlAll:String = tmp;
			var htmlBlock:String = "";
			
			var ptnTitle:RegExp  = /__TITLE__/; 
			var ptnTerm:RegExp = /__TERM__/; 
			var ptnDate0:RegExp = /__DATE0__/;
			var ptnDate1:RegExp = /__DATE1__/;
			var ptnDate2:RegExp = /__DATE2__/;
			var ptnDate3:RegExp = /__DATE3__/;
			var ptnDate4:RegExp = /__DATE4__/;
			var ptnPlace0:RegExp = /__PLACE0__/;
			var ptnPlace1:RegExp = /__PLACE1__/;
			var ptnPlace2:RegExp = /__PLACE2__/;
			var ptnPlace3:RegExp = /__PLACE3__/;
			var ptnPlace4:RegExp = /__PLACE4__/;
			var ptnPurpose:RegExp= /__PURPOSE__/;
			var ptnMemberCount:RegExp = /__MEMBER_COUNT__/;
			var ptnMemberBlock:RegExp = /__MEMBERS__/;  
			var ptnEtcBlock:RegExp = /__ETC__/;
			
			var ptnTrtype:RegExp= /__TRTYPE__/;
			
			
			htmlAll = htmlAll.replace(ptnTitle, summary.TRTITLE);
			htmlAll = htmlAll.replace(ptnPurpose, summary.PURPOSE);
			htmlAll = htmlAll.replace(ptnTerm, tripTerm());
			htmlAll = htmlAll.replace(ptnDate0, (placeList.length>0) ? DateUtil.toDefaultString(placeList.getItemAt(0).BEGDA): "");
			htmlAll = htmlAll.replace(ptnDate1, (placeList.length>1) ?  DateUtil.toDefaultString(placeList.getItemAt(1).BEGDA): "");
			htmlAll = htmlAll.replace(ptnDate2, (placeList.length>2) ?  DateUtil.toDefaultString(placeList.getItemAt(2).BEGDA): "");
			htmlAll = htmlAll.replace(ptnDate3, (placeList.length>3) ?  DateUtil.toDefaultString(placeList.getItemAt(3).BEGDA): "");
			htmlAll = htmlAll.replace(ptnDate4, (placeList.length>4) ?  DateUtil.toDefaultString(placeList.getItemAt(4).BEGDA): "");
			htmlAll = htmlAll.replace(ptnPlace0, (placeList.length>0) ?  placeList.getItemAt(0).BREN: "");
			htmlAll = htmlAll.replace(ptnPlace1, (placeList.length>1) ?  placeList.getItemAt(1).BREN: "");
			htmlAll = htmlAll.replace(ptnPlace2, (placeList.length>2) ?  placeList.getItemAt(2).BREN: "");
			htmlAll = htmlAll.replace(ptnPlace3, (placeList.length>3) ?  placeList.getItemAt(3).BREN: "");
			htmlAll = htmlAll.replace(ptnPlace4, (placeList.length>4) ?  placeList.getItemAt(4).BREN: "");
			
			htmlAll = htmlAll.replace(ptnMemberCount, memberList.length);
			htmlAll = htmlAll.replace(ptnMemberBlock, buildMemberBlock());
			htmlAll = htmlAll.replace(ptnEtcBlock, buildEtcBlock());
			
			
			//20140515
			htmlAll = htmlAll.replace(ptnTrtype, summary.TRTYPE=="01"?"시외출장":"국내출장");
			
			return htmlAll;
		}

		private function buildHTML2():String
		{
			var htmlAll:String = templateHTML2;
			var htmlBlock:String = "";
			
			var ptnAmount:RegExp = /__TOTAL_AMOUNT__/;
			var ptnMemberADjustBlock:RegExp = /__ADJUSTS__/;
			trace(htmlAll);
			
			htmlAll = buildHTML(htmlAll);
			
			trace(htmlAll.length);
			htmlAll = htmlAll.replace(ptnAmount, NumberUtil.NumberFormat(amount));
			htmlAll = htmlAll.replace(ptnMemberADjustBlock, buildMemberAjudstBlock());
			return htmlAll;
		}
		
		private function buildMemberBlock():String
		{
			var blocks:String = "";
			var openColumn:String = "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' height=30><P>";
			
			for each(var vo:BizTripMemberVO in memberList) {
				blocks += "<TR>" + openColumn + vo.ENAME + "</P></TD>";
				blocks += openColumn + vo.ORGTX + "</P></TD>";
				blocks += openColumn + DateUtil.toDefaultString(vo.BEGDA) + "~" + DateUtil.toDefaultString(vo.ENDDA) + "</P></TD>";
				blocks += openColumn + vo.WDAY + "</P></TD>";
				blocks += openColumn + ((vo.BIGO==null||vo.BIGO=="")? "-":vo.BIGO) + "</P></TD>";
				blocks += "</TR>";
			}
			return blocks
		}

		private function buildEtcBlock():String
		{
			var blocks:String = "";
			return blocks
		}


		private function buildMemberAjudstBlock():String
		{
			var blocks:String = "";
			var openColumn:String = "<TD style='background: #fdfdf3; text-align: center; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' height=30><P>";
			var openColumnRight:String = "<TD style='background: #fdfdf3; text-align: right; font-size: 9pt; border-bottom-color : #000000; border-bottom-style : solid; border-bottom-width : 1px; border-right-color : #000000; border-right-style : solid;    border-right-width : 1px;' height=30><P>";
			for each(var vo:MemberAdjustVO in memberAdjustList)
			{
//				blocks += "<TR>" + openColumn + vo.ENAME + "</P></TD>";
//				blocks += openColumnRight + NumberUtil.NumberFormat(vo.DAYPY) + "</P></TD>";
//				blocks += openColumnRight + NumberUtil.NumberFormat(vo.PCAPY) + "</P></TD>";
//				blocks += openColumnRight + NumberUtil.NumberFormat(vo.EATPY) + "</P></TD>";
//				blocks += openColumnRight + NumberUtil.NumberFormat(vo.STYPY) + "</P></TD>";
//				blocks += openColumnRight + NumberUtil.NumberFormat(vo.TRAPY) + "</P></TD>";
//				blocks += openColumnRight + NumberUtil.NumberFormat(vo.sumN) + "</P></TD>";
//				blocks += "</TR>";

				blocks += "<TR>" + openColumn + vo.ENAME + "</P></TD>";
				blocks += openColumnRight + NumberUtil.NumberFormat(vo.DAYPY) + "</P></TD>";
				blocks += openColumnRight+"&nbsp;</P></TD>";
				blocks += openColumnRight + NumberUtil.NumberFormat(vo.PCAPY) + "</P></TD>";				
				blocks += openColumnRight + NumberUtil.NumberFormat(vo.STYPY) + "</P></TD>";
				blocks += openColumnRight + NumberUtil.NumberFormat(vo.TRAPY) + "</P></TD>";
				blocks += openColumnRight + NumberUtil.NumberFormat(vo.sumN) + "</P></TD>";
				blocks += "</TR>";
			}
			return blocks;
		}

		private function tripTerm():String
		{
			return DateUtil.toDefaultString(summary.BEGDA) + " ~ " + DateUtil.toDefaultString(summary.ENDDA);
		}
		
		
	}
}